﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SpriteMaker.cs" company="melcoprog">
//   Copyright (c) melcoprog. All rights reserved.
// </copyright>
// <summary>
//   Defines the SpriteMaker type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CreatureSheep.Image
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;

    /// <summary>
    /// The sprite maker.
    /// </summary>
    public class SpriteMaker
    {
        /// <summary>
        /// The sprite list.
        /// </summary>
        private readonly Bitmap spriteList;

        /// <summary>
        /// The width.
        /// </summary>
        private readonly int width;

        /// <summary>
        /// The height.
        /// </summary>
        private readonly int height;

        /// <summary>
        /// Initializes a new instance of the <see cref="SpriteMaker"/> class.
        /// </summary>
        /// <param name="spriteList">
        /// The sprite list.
        /// </param>
        /// <param name="width">
        /// The width.
        /// </param>
        /// <param name="height">
        /// The height.
        /// </param>
        public SpriteMaker(Bitmap spriteList, int width, int height)
        {
            this.spriteList = spriteList;
            this.width = width;
            this.height = height;
        }

        /// <summary>
        /// Gets the width sprite count.
        /// </summary>
        private int WidthSpriteCount
        {
            get
            {
                return this.spriteList.Width / this.width;
            }
        }

        /// <summary>
        /// Gets the height sprite count.
        /// </summary>
        private int HeightSpriteCount
        {
            get
            {
                return this.spriteList.Height / this.height;
            }
        }

        /// <summary>
        /// The get sprite.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="Bitmap"/>.
        /// </returns>
        /// <exception cref="IndexOutOfRangeException">
        /// x or y
        /// </exception>
        public Bitmap GetSprite(int x, int y)
        {
            if (x < 0 || this.WidthSpriteCount < x)
            {
                throw new IndexOutOfRangeException("x");
            }

            if (y < 0 || this.HeightSpriteCount < y)
            {
                throw new IndexOutOfRangeException("y");
            }

            var rect = new Rectangle(x * this.width, y * this.height, this.width, this.height);

            return this.spriteList.Clone(rect, PixelFormat.Format32bppPArgb);
        }
    }
}
